<script setup lang="ts">
import { computed, ref } from 'vue';
import { useCultivationSkill, useRealm } from '../../../../composables';
import { EVENTS, GameKernel } from '../../../../game';

const { state: realmState } = useRealm();
const { state: cultivationSkillState } = useCultivationSkill();

const open = ref(false);
const title = ref('升级条件');

const currentKillId = ref('');

const currentSkill = computed(() => {
  const current =
    cultivationSkillState.value.cultivationSkillUpgradeProgressInfo.find(
      (item) => {
        return item.skill.skillId === currentKillId.value;
      }
    );
  return current;
});

const currentCultivationSkillUpgradeProgressInfo = computed(() => {
  const result =
    cultivationSkillState.value.cultivationSkillUpgradeProgressInfo.find(
      (item) => {
        return item.skill.skillId === currentKillId.value;
      }
    );
  if (!result) return [];
  return result.skillUpgradeInfo;
});
const currentLevelConfig = computed(() => {
  if (!currentSkill.value) return undefined;
  return currentSkill.value.skillConfig.levels[
    currentSkill.value.skill.currentLevel - 1
  ];
});
/** 经验足够 */
const hasEnoughExp = computed(() => {
  if (!currentSkill.value || !currentLevelConfig.value) return false;
  return currentLevelConfig.value.requiredExp <= realmState.value.currentExp;
});
const canUpgradeImmediately = computed(() => {
  const conditionPassed =
    currentCultivationSkillUpgradeProgressInfo.value.every((item) => {
      return item.isMet;
    });
  return conditionPassed && hasEnoughExp.value;
});
const skillUpgradeRequiredExp = computed(() => {
  if (!currentSkill.value || !currentLevelConfig.value) return 0;
  return currentLevelConfig.value.requiredExp;
});
const isMaxLevel = computed(() => {
  if (!currentSkill.value || !currentLevelConfig.value) return false;
  return (
    currentSkill.value.skill.currentLevel >=
    currentSkill.value.skillConfig.maxLevel
  );
});

function show(skillId: string) {
  GameKernel.inst.bus.emit(EVENTS.CULTIVATION_SKILL_UPDATE_UPGRADE_INFO);
  currentKillId.value = skillId;
  if (!currentKillId.value) return;
  open.value = true;
}

function close() {
  open.value = false;
}

function onUpgradeClick() {
  if (!currentKillId.value) return;
  GameKernel.inst.bus.emit(
    EVENTS.CULTIVATION_SKILL_UPGRADE,
    currentKillId.value
  );
}

defineExpose({
  show,
  close,
});
</script>

<template>
  <a-modal
    v-model:open="open"
    wrap-class-name="cultivation-kill-upgrade-modal"
    :centered="true"
    :closable="false"
    :footer="null"
  >
    <template #title>
      <div class="w-full text-center">{{ title }}</div>
    </template>
    <div>
      <div v-if="!isMaxLevel">
        <div class="flex items-center">
          <div>消耗修为：</div>
          <div>
            {{ Math.floor(realmState.currentExp) }}/{{
              Math.floor(skillUpgradeRequiredExp) || '∞'
            }}
          </div>
        </div>
        <div
          v-for="item in currentCultivationSkillUpgradeProgressInfo"
          class="flex items-center"
        >
          <div>{{ item.description }}</div>
        </div>
      </div>
      <div v-else>已达到最大等级</div>
      <div class="flex justify-end flex-wrap gap-2 pt-2">
        <a-button
          v-if="!isMaxLevel"
          :disabled="!canUpgradeImmediately"
          @click="onUpgradeClick"
        >
          {{
            currentCultivationSkillUpgradeProgressInfo.length > 0
              ? '突破'
              : '升级'
          }}
        </a-button>
        <a-button @click="close">关闭</a-button>
      </div>
    </div>
  </a-modal>
</template>

<style>
.cultivation-kill-upgrade-modal .ant-modal-content {
  padding: 16px;
}
</style>
